package association

import (
	"gorm.io/gorm"
)

func QueryWithBelongsTo(db *gorm.DB) {
	// #region define
	type Company struct {
		ID   uint `gorm:"primarykey"`
		Name string
	}
	type User struct {
		ID        int `gorm:"primarykey"`
		Name      string
		CompanyID int
		Company   Company
	}
	// #endregion define

	// #region create
	var user = User{
		Name:    "user1",
		Company: Company{Name: "company1"},
	}
	db.AutoMigrate(user)
	db.Create(&user)
	db.Save(&user)
	// #endregion create

	// #region query
	var firstUser = User{}
	db.Preload("Company").First(&firstUser, 1)
	// #endregion query

	// #region delete
	db.Select("User").Delete(&Company{}, 1)
	// #endregion delete
}
